/**
 * 
 */
package com.heer.RD.projecthour.page.include;

import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;

import com.heer.RD.auth.HeerSession;
import com.heer.RD.projecthour.page.LoginPage;
import com.heer.RD.projecthour.page.SysMngPage;
import com.heer.RD.projecthour.pojo.UserInfoDTO;

/**
 * @author ZhaoZehui
 *
 */
public class HeaderPanel extends Panel {

	public HeaderPanel(String id, WebPage page) {
		super(id);

		Link loginLink = new Link("loginLink") {
			@Override
			public void onClick() {
				setResponsePage(LoginPage.class);
			}

			@Override
			public boolean isVisible() {
				return !HeerSession.session().isSignIn();
			}
		};
		Link logoutLink = new Link("logoutLink") {
			@Override
	        public void onClick() {
	            Session.get().invalidateNow();
	            setResponsePage(getApplication().getHomePage());
	        }

	        @Override
	        public boolean isVisible() {
	            return HeerSession.session().isSignIn();
	        }
		};
		
		final ModalWindow settingDlg = new ModalWindow("settingDlg");
		settingDlg.setTitle("个人项目工时明细");
		settingDlg.setCookieName("PERSON_SETTING_DIALOG");
		settingDlg.setInitialWidth(600);
		settingDlg.setInitialHeight(480);
		settingDlg.setWidthUnit("px");
		settingDlg.setHeightUnit("px");
		settingDlg.setCloseButtonCallback(new ModalWindow.CloseButtonCallback()	{
			public boolean onCloseButtonClicked(AjaxRequestTarget target) {
				return true;
			}
		});
		add(settingDlg);
		
		AjaxLink modifyInfoLink = new AjaxLink("modifyInfoLink") {
	        @Override
	        public boolean isVisible() {
	            return HeerSession.session().isSignIn();
	        }

			@Override
			public void onClick(AjaxRequestTarget target) {
				settingDlg.setTitle("设置个人信息");
				settingDlg.setContent(new SysMngPage(null).getUserUpdatePanel(settingDlg.getContentId(), HeerSession.session().getUser().getUserId()));
				settingDlg.show(target);
			}
		};
		
		if(HeerSession.session().isSignIn()) {
			UserInfoDTO user = HeerSession.session().getUser();
			add(new Label("loginName", "欢迎 " + user.getName() + " "));
			add(new Label("separator1", "|"));
			add(new Label("separator2", "|"));
		}
		else {
			add(new Label("loginName", "").setVisible(false));
			add(new Label("separator1").setVisible(false));
			add(new Label("separator2").setVisible(false));
		}
		
		add(loginLink);
		add(logoutLink);
		add(modifyInfoLink);
		
		
	}

}
